package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.order.MarketOrder;
import com.barbeque.mall.bean.order.MarketOrderExample;
import com.barbeque.mall.bean.order.MarketOrderGoods;
import com.barbeque.mall.bean.order.MarketOrderGoodsExample;
import com.barbeque.mall.bean.wx.aftersale.MarketAftersale;
import com.barbeque.mall.bean.wx.aftersale.MarketAftersaleCommitBo;
import com.barbeque.mall.bean.wx.aftersale.MarketAftersaleDetaiVo;
import com.barbeque.mall.bean.wx.aftersale.MarketAftersaleExample;
import com.barbeque.mall.mapper.MarketAftersaleMapper;
import com.barbeque.mall.mapper.MarketOrderGoodsMapper;
import com.barbeque.mall.mapper.MarketOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Tezel
 * @since 2022/07/21 09:58
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class WxAftersaleServiceImpl implements WxAftersaleService {

    @Autowired
    MarketAftersaleMapper aftersaleMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Override
    public void commit(MarketAftersaleCommitBo aftersaleCommitBo) {

        //aftersale新增一条数据

        MarketAftersale marketAftersale = new MarketAftersale();
        marketAftersale.setAmount(aftersaleCommitBo.getAmount());
        marketAftersale.setOrderId(aftersaleCommitBo.getOrderId());
        marketAftersale.setPictures(aftersaleCommitBo.getPictures());
        marketAftersale.setType(aftersaleCommitBo.getType());
        marketAftersale.setReason(aftersaleCommitBo.getReason());

        //todo 获取用户id
        marketAftersale.setUserId(1);

        Date date = new Date(System.currentTimeMillis());

        marketAftersale.setAddTime(date);
        marketAftersale.setUpdateTime(date);
        marketAftersale.setDeleted(false);
        marketAftersale.setStatus(new Short("1"));

        //生成随机售后单号
        String str = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Random random = new Random();
        int num = (int) (random.nextDouble() * (999999 - 100000 + 1)) + 100000;

        String aftersale = str + num;

        marketAftersale.setAftersaleSn(aftersale);


        aftersaleMapper.insertSelective(marketAftersale);

        //修改订单aftersale状态
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(aftersaleCommitBo.getOrderId());

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setAftersaleStatus(new Short("1"));
        marketOrder.setUpdateTime(date);

        orderMapper.updateByExampleSelective(marketOrder, orderExample);

    }


    @Override
    public MarketAftersaleDetaiVo detail(Integer orderId) {

        //order结果 todo userId
        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);

        //通过orderId查 goods

        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();

        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();

        criteria.andOrderIdEqualTo(orderId);

        //orderGoods结果
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        MarketAftersaleExample aftersaleExample = new MarketAftersaleExample();

        MarketAftersaleExample.Criteria criteria1 = aftersaleExample.createCriteria();

        criteria1.andOrderIdEqualTo(orderId);

        List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(aftersaleExample);
        MarketAftersale marketAftersale = marketAftersales.get(0);


        MarketAftersaleDetaiVo marketAftersaleDetaiVo = new MarketAftersaleDetaiVo();

        marketAftersaleDetaiVo.setOrder(order);
        marketAftersaleDetaiVo.setOrderGoods(marketOrderGoods);
        marketAftersaleDetaiVo.setAftersale(marketAftersale);


        return marketAftersaleDetaiVo;

    }
}
